<template>
  <div>
    <!-- 上牌信息查询 -->
    <TopFilter
      :list="topFilterList"
      :cols="3"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
      :labelWidth="110"
    ></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :isSelectColumn="false"
        columnsRef="zjRegistrationInquirys"
        :onRowSelectChange="selectChangeHandle"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" @click="addHandle()">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <Drawer
      :visible.sync="visible"
      :title="'详情'"
      destroyOnClose
      :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }"
      width="85%"
    >
      <childrenModel :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import { getList, updatePlateApply,exporturl, exportList, vehicleConfiguration } from '@/api/dealerSale/Registration/RegistrationInquiry';
import { dictionary } from '@/utils/dictMixin';
import childrenModel from './childrenModel';
import { exportExcelFile } from '@/utils';
export default {
  name: 'TemplateDistribution',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        xhrAbort: true,
        statusList: [60191002, 60191003, 60191004]
      },
      fetchApi: getList,
      formData: {},
      exportLoading: false,
      visible: false
    };
  },
  components: {
    childrenModel
  },
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getList();
    this.queryYearInfo();
  },
  methods: {
    // 获取当前年月日跟上年年份
    queryYearInfo() {
      let date = new Date();
      let thisYear = date.getFullYear();
      let thisMonth = date.getMonth() + 1;
      if (thisMonth == 13) {
        thisMonth = 1;
        thisYear = thisYear + 1;
      }

      debugger;
      let stDate = date.getFullYear() + '-' + thisMonth + '-01';
      let endDate = thisYear + '-' + thisMonth + '-' + date.getDate();
      let dateValue = [stDate, endDate];
      this.topFilterList.find(item => item.fieldName === 'startSalesDate|endSalesDate').initialValue = dateValue;
    },
    createdFrom() {
      return [
        {
          type: 'RANGE_DATE',
          label: '首次提报日期',
          fieldName: 'startReportDate|endReportDate',
          dateType: 'exactdaterange',
          placeholder: '请选择',
          initialValue: []
        },
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        },
        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'customerName',
          placeholder: '请输入'
        },
        {
          type: 'INPUT_CASCADER',
          label: '车型选择',
          fieldName: 'vehicleInfo',
          placeholder: '请选择',
          options: { titles: ['品牌', '车系', '车型', '配置', '选装'] },
          itemList: []
        },
        {
          type: 'SELECT',
          label: '上牌审批状态',
          fieldName: 'checkStatus',
          placeholder: '请选择',
          itemList: [
            { text: '提交', value: 60191002 },
            { text: '审核通过', value: 60191003 },
            { text: '审核驳回', value: 60191004 }
          ]
        },
        {
          type: 'RANGE_DATE',
          label: '实销审核通过日期',
          fieldName: 'startSalesDate|endSalesDate',
          dateType: 'exactdaterange',
          placeholder: '请选择',
          initialValue: []
        },
        {
          type: 'RANGE_DATE',
          label: '行驶证注册日期',
          fieldName: 'startRegisterDate|endRegisterDate',
          dateType: 'exactdaterange',
          placeholder: '请选择',
          initialValue: []
        },
        {
          type: 'RANGE_DATE',
          label: '上牌审核日期',
          fieldName: 'startCheckDate|endCheckDate',
          dateType: 'exactdaterange',
          placeholder: '请选择',
          initialValue: []
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  详情
                </multiuse-button>
              </div>
            );
          }
        },

        {
          title: '上牌审批状态',
          dataIndex: 'checkStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6019'),
          render: p => {
            return <span>{this.createDictText(p.row.checkStatus, '6019')}</span>;
          }
        },
        {
          title: '零售单号',
          dataIndex: 'retailOrderNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户编码',
          dataIndex: 'potentialCustomerNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'customerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '性别',
          dataIndex: 'customerSex',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1002'),
          render: p => {
            return <span>{this.createDictText(p.row.customerSex, '1002')}</span>;
          }
        },
        {
          title: '手机号',
          dataIndex: 'mobile',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '电话',
          dataIndex: 'phone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '使用人',
          dataIndex: 'userName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '使用人电话',
          dataIndex: 'userPhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '身份证号',
          dataIndex: 'cardNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发票号',
          dataIndex: 'invoiceNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '开票价格',
          dataIndex: 'invoiceAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '开票日期',
          dataIndex: 'invoiceDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发票附件',
          dataIndex: 'fpfj',
          sorter: true,
          render: p => {
            let list = p.row.enclosures || [];
            let array = [];
            if (list.length > 0) {
              let arr = [];
              for (let i = 0; i < list.length; i++) {
                if (list[i].enclosureType == 'invoiceAddress') {
                  array.push({ text: list[i].enclosureName, value: list[i].enclosureUrl });
                }
              }
            }
            return (
              <div>
                {array.map(v => {
                  return (
                    <div>
                      <a style="color:#bb0a30" href={v.value} target="_blank" download>
                        {v.text}
                      </a>
                    </div>
                  );
                })}
              </div>
            );
          }
        },
        {
          title: '行驶证附件',
          dataIndex: 'xszfj',
          sorter: true,
          render: p => {
            let list = p.row.enclosures || [];
            let array = [];
            if (list.length > 0) {
              let arr = [];
              for (let i = 0; i < list.length; i++) {
                if (list[i].enclosureType == 'drivingAddress') {
                  array.push({ text: list[i].enclosureName, value: list[i].enclosureUrl });
                }
              }
            }
            return (
              <div>
                {array.map(v => {
                  return (
                    <div>
                      <a style="color:#bb0a30" href={v.value} target="_blank" download>
                        {v.text}
                      </a>
                    </div>
                  );
                })}
              </div>
            );
          }
        },
        {
          title: '车牌号',
          dataIndex: 'plateNumber',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '行驶证注册日期',
          dataIndex: 'drivingRegisteDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '车系名称',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型名称',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置名称',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装名称',
          dataIndex: 'optionName',
          align: 'center',
          width: 250,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '颜色',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '公告号',
          dataIndex: 'announceNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '上牌省份',
          dataIndex: 'plateProvinceName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '上牌城市',
          dataIndex: 'plateCityName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '上牌审核日期',
          dataIndex: 'checkDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '首次实销提报日期',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '实销通过日期',
          dataIndex: 'reportUpdatedAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '首次上牌提报日期',
          dataIndex: 'applyDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '出厂日期',
          dataIndex: 'productDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '入库日期',
          dataIndex: 'postingDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '交车日期',
          dataIndex: 'deliveryDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },

        {
          title: '市场指导价',
          dataIndex: 'buyPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发动机',
          dataIndex: 'engineNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '电机号',
          dataIndex: 'motorNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '电池号',
          dataIndex: 'batteryNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '下线工厂',
          dataIndex: 'factoryName',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    selectChangeHandle(row) {},
    editHandle(data) {
      this.formData = data;
      this.visible = true;
    },
    async getList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'vehicleInfo').itemList = res.data;
    },
    async addHandle(type) {
           let json = Object.assign({}, { CODE: 'CODE_0000015' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     let json = this.fetchParams;
      //     this.exportLoading = true;

      //     exportList(json)
      //       .then(response => {
      //         exportExcelFile(response.data, '上牌信息查询');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     console.log('456');
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params, { xhrAbort: false });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    emitcloseHandler() {
      this.visible = false;
    }
  }
};
</script>
